﻿using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using EmptyWallGallery.Core;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.FrontEnd.inside.Editor.Video
{
    public partial class Create : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Thêm mới video - " + Configs.WebTitle;
        }

        protected void BtnNext_OnClick(object sender, EventArgs e)
        {
            var url =
                string.Format(
                    (string) "http://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D{0}&amp;format=json",
                    (object) txtUrl.Text);

            var obj = Spider.Instance.GetStreamCntWeb(url);
            if (obj == null)
            {
                test.Visible = true;
                test.InnerHtml = "<font color='red'>* Video không tồn tại.</font>";
                return;
            }
            
            var serializer = new DataContractJsonSerializer(typeof(YoutubeVideo));
            var ytVideo = (YoutubeVideo)serializer.ReadObject(obj);

            txtTitle.Text = ytVideo.title;
            imgVideo.ImageUrl = ytVideo.thumbnail_url;

            titleStep.InnerText = "Thêm mới Video - Bước 2";
            divStep2.Visible = true;
            divStep1.Visible = false;
            test.Visible = false;
        }

        protected void BtnBack_OnClick(object sender, EventArgs e)
        {
            titleStep.InnerText = "Thêm mới Video - Bước 1";
            divStep2.Visible = false;
            divStep1.Visible = true;
        }

        protected void BtnCreate_OnClick(object sender, EventArgs e)
        {
            var rnd = new RandomStrings();
            var strFileName = rnd.GenerateRandomString(DateTime.Now) + ".jpg";
            var strFolderUpload = Configs.UserFolder + HttpContext.Current.User.Identity.Name + @"\" + DateTime.Now.Year + @"\" +
                                  Common.Instance.Right("0" + DateTime.Now.Month, 2) + @"\" +
                                  Common.Instance.Right("0" + DateTime.Now.Day, 2) + @"\";
            if (!Directory.Exists(strFolderUpload))
            {
                Directory.CreateDirectory(strFolderUpload);
            }

            var objImage = Spider.Instance.GetStreamCntWeb(imgVideo.ImageUrl);
            var b = objImage != null && ImageUtilities.ResizeImage(objImage, strFolderUpload + "100x0__" + strFileName, 100, 0, "");

            var membershipUser = Membership.GetUser();
            if (membershipUser != null)
                if (membershipUser.ProviderUserKey != null)
                    Core.BLL.Videos.Instance.Create(txtTitle.Text, Server.HtmlEncode(txtBody.Value), txtUrl.Text, strFileName,
                                                    drpStatus.SelectedValue == "1",
                                                    membershipUser.ProviderUserKey.ToString());
            Response.Redirect("Default.aspx");
        }
    }

    public class YoutubeVideo
    {
        public string title { get; set; }
        public string thumbnail_url { get; set; }
    }
}